-- value_log
--
local import = import
---@type behavior_ret
local bret = import('bt/bt_plugin/behavior3/behavior_ret')

local M = {
    name = "value_log",
    type = "Action",
    desc = "打印变量",
    args = {
        --{ "str", "string", "变量描述" }
    },
    input = { "变量名" }
}

local function printTable(tbl, indent)
    indent = indent or 0
    for k, v in pairs(tbl) do
        local padding = string.rep(" ", indent)
        if type(v) == "table" then
            if table.is_empty(v) then
                xxmsg(padding .. k .. " = { nil }")
            else
                xxmsg(padding .. k .. " = {")
                printTable(v, indent + 2)
                xxmsg(padding .. "}")
            end
        else
            xxmsg(padding .. k .. " = " .. tostring(v))
        end
    end
end

function M.run(node, env, value)
    if print_r then
        print_r(value)
        return bret.SUCCESS
    end
    if value then
        if type(value) == "table" then
            printTable(value)
        else
            xxmsg(value)
        end
        return bret.SUCCESS
    else
        return bret.FAIL
    end
end

return M
